<template>
  <el-dialog title="新增文章" :visible="showDialog" @close="handleClose">
    <!-- 表单 -->
    <el-form ref="addDeptRef" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="formData.title" style="width:80%" placeholder="请输入文章标签" />
      </el-form-item>

      <el-form-item label="文章内容" prop="articleBody">
        <el-input v-model="formData.articleBody" style="width:80%" placeholder="请输入文章内容" />
      </el-form-item>

      <el-form-item label="视频地址" prop="videoURL">
        <el-input v-model="formData.videoURL" style="width:80%" placeholder="请输入视频地址" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { add } from '@/api/hmmm/articles.js'
export default {
  data () {
    return {
      formData: {
        title: '', // 文章标题
        articleBody: '', // 文章内容
        videoURL: '' // 视频地址
      },
      rules: {
        title: [
          { required: true, message: '文章标题不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 16, message: '文章标题为1-12=6位', trigger: ['blur', 'change'] }
        ],
        articleBody: [
          { required: true, message: '文章内容不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 1000, message: '文章标题为1-1000位', trigger: ['blur', 'change'] }
        ]
        // videoURL: [
        //   { required: true, message: '视频地址不能为空', trigger: ['blur', 'change'] }
        // ]
      }
    }
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // 添加部门提交表单
    handleSubmit () {
      this.$refs.addDeptRef.validate(async valid => {
        if (!valid) return
        try {
          const ret = await add(this.formData)
          if (ret.status === 200) {
            // 添加成功 关闭弹窗 刷新列表
            this.$emit('update:showDialog', false)
            // 刷新列表 this.$parent 表示当前组件的父组件实例
            this.$parent.loadList()
            this.$message.success('添加文章成功')
          }
        } catch {
          this.$message.error('添加文章失败')
        }
      })
    },
    // 关闭弹窗
    handleClose () {
      this.$emit('update:show-dialog', false)
      this.$refs.addDeptRef.resetFields()
    }
  }
}
</script>

<style>
.a {
  height: 100px;
}
</style>
